#include "mainwindow.h"
#include "gameview.h"
#include "gamepage.h"
#include "fadestackwidget.h"

#include <QLabel>
#include <QTimer>
#include <QApplication>
#include <QFile>

MainWindow::MainWindow() : QMainWindow(nullptr) {
    setWindowTitle("ThunderRaid");
    setWindowIcon(QIcon(":/app.ico"));
    setFixedSize(512, 768);

    stack = new FadeStackWidget;
    setCentralWidget(stack);

    mainPage = new MainPage;
    connect(mainPage, &MainPage::startGame, this, &MainWindow::choiceDifficult);
    connect(mainPage, &MainPage::resumeGame, this, &MainWindow::resumeGame);
    connect(mainPage, &MainPage::showHelp, this, &MainWindow::showHelp);
    connect(mainPage, &MainPage::quitGame, qApp, &QApplication::quit, Qt::QueuedConnection);
    stack->addWidget(mainPage);

    game = new GameView;
    connect(game, &GameView::wantChangeLevel, this, &MainWindow::levelChanged);
    connect(game, &GameView::gameFinished, this, [this](bool isSuccess, int score){
        endPage->setState(isSuccess, score);
        stack->switchTo(endPage);
    });
    connect(game, &GameView::gamePause, this, [this]{
        game->clearFocus();
        stack->switchTo(pausePage);
    });
    stack->addWidget(game);

    levelPage = new LevelPage;
    stack->addWidget(levelPage);

    endPage = new EndPage;
    connect(endPage, &EndPage::restartGame, this, &MainWindow::choiceDifficult);
    connect(endPage, &EndPage::backToMain, this, [this]{ stack->switchTo(mainPage); });
    connect(endPage, &EndPage::quitGame, qApp, &QApplication::quit, Qt::QueuedConnection);
    stack->addWidget(endPage);

    pausePage = new PausePage;
    connect(pausePage, &PausePage::resumeGame, this, [this]{stack->switchTo(game);game->start();});
    connect(pausePage, &PausePage::saveGame,game, &GameView::saveGame);
    connect(pausePage, &PausePage::endGame, this, [this]{stack->switchTo(mainPage);game->end();mainPage->reload();});
    stack->addWidget(pausePage);

    helpPage = new HelpPage;
    connect(helpPage, &HelpPage::backToMain, this, [this]{ stack->switchTo(mainPage); });
    stack->addWidget(helpPage);

    difficult = new DifficultChoicePage;
    connect(difficult, &DifficultChoicePage::selectDifficult, this, &MainWindow::startWithDifficult);
    stack->addWidget(difficult);

    stack->setCurrentWidget(mainPage);
}
void MainWindow::choiceDifficult() {
    stack->switchTo(difficult);
}
void MainWindow::resumeGame() {
    game->loadSave();
}
void MainWindow::showHelp() {
    stack->switchTo(helpPage);
}
void MainWindow::levelChanged(const GameLevel &level) {
    levelPage->setLevel(level);
    stack->switchTo(levelPage);
    auto *dummy = new QObject;
    connect(stack, &FadeStackWidget::changeFinished, dummy, [this, dummy, &level]{
        dummy->deleteLater();
        game->setLevel(level);
        stack->switchTo(game);
        game->setFocus();
        game->start();
    });
}
void MainWindow::startWithDifficult(DifficultChoicePage::Difficult difficult) {
    game->reset();
    game->setScore(0);
    game->applyToSetLevel(GameLevel::levelList["official1"]);
}
